package model;

import java.sql.ResultSet;
import java.util.ArrayList;
import database.RoadOperations;

public class RoadCollection 
{
	private ArrayList<Road> roadlist;
	private ResultSet rst;
	private RoadOperations ro;
	public RoadCollection(RoadOperations r)
	{
		this.ro=r;
		roadlist=new ArrayList<Road>();
	}
	
	
	
	public Road getRoad(int i) 
	{
		return roadlist.get(i);
	}

	public int getNumRoads() 
	{
		return roadlist.size();
	}
	
	public boolean removeRoad(int id) 
	{
		boolean ans=false;
		for (int i = 0; i < roadlist.size(); i++) 
		{
			if (id==roadlist.get(i).getId()) 
			{
				roadlist.remove(i);
				ro.deleteRoad(id);
				ans= true;
			}
		}
		return ans;
	}
	
	public void addRoad()
	{
		rst = ro.getLastRow();
		try 
		{
			Road r = new Road(rst.getInt(1), rst.getInt(2),rst.getString(3));
			roadlist.add(r);
			
		} 
		catch (Exception e) 
		{
			System.out.println(e);
		}
	}
	
	
	public int findRoad(int id) 
	{
		int index = -1;
		for (int i = 0; i < roadlist.size(); i++) 
		{
			if (id==roadlist.get(i).getId()) 
			{
				index = i;
			}
		}
		return index;
	}
}
